package a07_二叉树;

import a05_双指针法.ListNode;

import java.math.BigDecimal;
import java.text.Bidi;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 * a03_层序遍历01_二叉树的层序遍历
 * </p>
 *
 * @author flyduck
 * @since 2024-08-20
 */
public class a05_层序遍历01_二叉树的层序遍历 {

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("7");
        System.out.println(bigDecimal.compareTo(BigDecimal.ZERO));
//        TreeNode root = new TreeNode(
//                6,
//                new TreeNode(4,new TreeNode(1),new TreeNode(3)),
//                new TreeNode(7,new TreeNode(5),new TreeNode(8)));
//        TreeNode root = new TreeNode(
//                6,
//                new TreeNode(4),
//                new TreeNode(7,new TreeNode(5),new TreeNode(8)));
//        List<List<Integer>> lists = levelOrder(root);
//        System.out.println(lists);
    }


    //      6
    //  4       7
    //1   3   5   8
    //queue里面有n个元素

    //吐出一个，将它的左右孩子加入队列
    //size--
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> resultList = new ArrayList<>();
        if(root == null){
            return resultList;
        }

        Queue<TreeNode> queue = new LinkedList<>();

        queue.add(root);


        while (queue.size() > 0) {
            List<Integer> result = new ArrayList<>();
            int popSize = queue.size();//当前层级要吐出的元素的个数
            while (popSize > 0) {
                TreeNode poll = queue.poll();
                result.add(poll.val);
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if (poll.right != null) {
                    queue.add(poll.right);
                }
                popSize--;
            }
            resultList.add(result);//吐完当前层级后，将result加入到resultList
            popSize = queue.size();//下一个层级要吐出的元素的个数
        }

        return resultList;
    }
}
